//
//  XJWelcomeController.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/19.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit
import SDWebImage

class XJWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self .setupUI()
    }

    // MARK: - 布局页面
    func setupUI() {
    
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        
        view.addSubview(iconImage)
        view.addSubview(msgLabel)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            make.height.width.equalTo(80)
            make.top.equalTo(200)
        }
        
        msgLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImage)
            make.top.equalTo(iconImage.snp_bottom).offset(20)
        }
        
    
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //更新约束
        iconImage.snp_updateConstraints(closure: { (make) -> Void in
            
            make.top.equalTo(100);
            
            })
        
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view .layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.msgLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //利用通知,切换控制器(appdelegate为通知的观察者)
                        NSNotificationCenter.defaultCenter().postNotificationName(XJChangeRootVCNotification, object: self)
                })
        }
    }
    
    
    // MARK: - 懒加载控件
    private lazy var iconImage: UIImageView = {
        
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 40
        image.layer.masksToBounds = true
        
        //加载头像
        if let img = XJUserAccountModel.sharedModel.account?.avatar_large {
            
            image.sd_setImageWithURL(NSURL(string: img))
        }
        
        return image
    }()
    
    private lazy var msgLabel: UILabel = {
        
        let messageLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        messageLabel.text = "欢迎回来"
        messageLabel.alpha = 0
        
        return messageLabel
    
    }()
}
